package com.dbxt.common.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dbxt.common.service.CommonService;
import com.dbxt.entity.Clerk;
import com.dbxt.entity.Student;
import com.dbxt.entity.Teacher;
import com.dbxt.util.MD5Util;

@Controller
@RequestMapping("/common")
public class CommonController {
	@Autowired
	private CommonService commonService;
	
@RequestMapping("/login")
@ResponseBody
public JSONObject login(String username,String password,String type,HttpSession session) {
	JSONObject json=new JSONObject();
	int status=1;
	if(type==null) {
		json.put("status", 0);
		json.put("message", "登录类型未选择");
		return json;
	}
	switch(type) {
	case "student":
		//学生登录方法
		Student student = commonService.studentLogin(username, password);
		if(student!=null) {
			session.setAttribute("student", student);
			json.put("status", 0);
			json.put("message", "操作成功");
			json.put("sid", student.getSid());
			json.put("sno", student.getSno());
			json.put("name", student.getName());
			json.put("grade", student.getGrade());
			json.put("major", student.getMajor());
			json.put("clazz", student.getClazz());
			json.put("college", student.getCollege());
			json.put("picture",student.getPicture());
		}else {
			json.put("status", status);
			json.put("message", "账号密码错误");
		}
		break;
	case "teacher":
		//老师登录方法
		Teacher teacher = commonService.teacherLogin(username, password);
		if(teacher!=null) {
			session.setAttribute("teacher", teacher);
			json.put("status", 0);
			json.put("message", "操作成功");
			json.put("tid", teacher.getTid());
			json.put("username", teacher.getUsername());
			json.put("name", teacher.getName());
			json.put("group",teacher.getGrouping());
		}else {
			json.put("status", status);
			json.put("message", "账号密码错误");
		}
		break;
	case "clerk":
		//书记员登录方法
		Clerk clerk = commonService.clerkLogin(username, password);
		if(clerk!=null) {
			session.setAttribute("clerk", clerk);
			json.put("status", 0);
			json.put("message", "操作成功");
			json.put("cid", clerk.getCid());
			json.put("username", clerk.getUsername());
			json.put("name",clerk.getName());
		}else {
			json.put("status", status);
			json.put("message", "账号密码错误");
		}
		break;
		//默认情况
		default:json.put("status", status);
		json.put("message", "账号密码错误");
		break;
	}
	return json;
	
}
@RequestMapping(value = "/logout",method = RequestMethod.GET)
@ResponseBody
public JSONObject logout(HttpSession session) {
	JSONObject json=new JSONObject();
	int status=1;
	try {
		session.removeAttribute("student");
		session.removeAttribute("teacher");
		session.removeAttribute("clerk");
		session.invalidate();
	} catch (Exception e) {
		json.put("status", status);
		json.put("messsage", "操作失败");
		e.printStackTrace();
	}
	json.put("status", 0);
	json.put("messsage", "操作成功");
	return json;
}
//修改密码
@RequestMapping(value = "/changepassword",method = RequestMethod.POST)
@ResponseBody
public JSONObject changepassword(HttpSession session,String password) throws Exception {
	JSONObject json=new JSONObject();
	int flag=-1;
	int status=1;
	if("".equals(password)||password==null) {
		json.put("status", status);
		json.put("message", "输入密码为空");
		return json;
	}
	String keyAndPassword = MD5Util.md5withkey(password, "dbxt");
	Student student = (Student) session.getAttribute("student");
	Clerk clerk = (Clerk) session.getAttribute("clerk");
	Teacher teacher = (Teacher) session.getAttribute("teacher");
	//都为空，都没有登录
	if(teacher==null&&clerk==null&&student==null) {
		json.put("status", 2);
		json.put("message", "操作失败");
		return json;
	}
	if(teacher!=null) {
		flag=2;
	}
	if(clerk!=null) {
		flag=3;
	}
	if(student!=null) {
		flag=4;
	}
	switch(flag) {
	case 2:
		//执行修改老师
		commonService.changeTeacherPassword(keyAndPassword, teacher.getTid());
		json.put("status", 0);
		json.put("message", "操作成功");
		break;
	case 3:
		//执行修改书记员
		commonService.changeClerkPassword(keyAndPassword, clerk.getCid());
		json.put("status", 0);
		json.put("message", "操作成功");
		break;
	case 4:
		//执行修改学生
		commonService.changeStudentPassword(keyAndPassword, student.getSid());
		json.put("status", 0);
		json.put("message", "操作成功");
		break;
	default:
		json.put("status", 2);
		json.put("message", "操作失败");
		break;
	}
	return json;
}
}
